﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Workflow.Runtime

Namespace System.Workflow.Activities
    <Serializable> _
    Public NotInheritable Class EventQueueName
        Implements IComparable
        ' Methods
        Public Sub New(ByVal interfaceType As Type, ByVal operation As String)
            If (interfaceType Is Nothing) Then
                Throw New ArgumentNullException("interfaceType")
            End If
            If (operation Is Nothing) Then
                Throw New ArgumentNullException("operation")
            End If
            Me.interfaceType = interfaceType
            Me.operation = operation
        End Sub

        Public Sub New(ByVal interfaceType As Type, ByVal operation As String, ByVal propertyValues As ICollection([Of] CorrelationProperty))
            Me.New(interfaceType, operation)
            If (Not propertyValues Is Nothing) Then
                Me.correlationValues = New CorrelationProperty(propertyValues.Count  - 1) {}
                propertyValues.CopyTo(Me.correlationValues, 0)
            End If
        End Sub

        Friend Sub New(ByVal interfaceType As Type, ByVal operation As String, ByVal activityId As String)
            Me.New(interfaceType, operation)
            Me.activityId = activityId
        End Sub

        Public Function CompareTo(ByVal toCompare As Object) As Integer
            If TypeOf toCompare Is EventQueueName Then
                Return Me.CompareTo([TryCast](toCompare,EventQueueName))
            End If
            Return -1
        End Function

        Public Function CompareTo(ByVal eventQueueName As EventQueueName) As Integer
            If (eventQueueName Is Nothing) Then
                Return -1
            End If
            Dim num As Integer = StringComparer.Ordinal.Compare(Me.MethodName, eventQueueName.MethodName)
            If (num = 0) Then
                num = StringComparer.Ordinal.Compare(Me.AssemblyQualifiedName, eventQueueName.AssemblyQualifiedName)
                If ((num <> 0) OrElse (Me.correlationValues Is Nothing)) Then
                    Return num
                End If
                num = If((Not eventQueueName.correlationValues Is Nothing), (Me.correlationValues.Length - eventQueueName.correlationValues.Length), -1)
                If (num <> 0) Then
                    Return num
                End If
                Dim i As Integer
                For i = 0 To Me.correlationValues.Length - 1
                    If ((Me.correlationValues(i) Is Nothing) OrElse (eventQueueName.correlationValues(i) Is Nothing)) Then
                        Return -1
                    End If
                    Dim obj2 As Object = Me.correlationValues(i).Value
                    Dim obj3 As Object = Me.FindCorrelationValue(Me.correlationValues(i).Name, eventQueueName.correlationValues)
                    If ((Not obj2 Is Nothing) OrElse (Not obj3 Is Nothing)) Then
                        If (obj2 Is Nothing) Then
                            Return -1
                        End If
                        Dim comparable As IComparable = [TryCast](obj2,IComparable)
                        If (Not comparable Is Nothing) Then
                            num = comparable.CompareTo(obj3)
                            If (num <> 0) Then
                                Return num
                            End If
                        ElseIf Not obj2.Equals(obj3) Then
                            Return -1
                        End If
                    End If
                Next i
            End If
            Return num
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim eventQueueName As EventQueueName = [TryCast](obj,EventQueueName)
            If (eventQueueName Is Nothing) Then
                Return False
            End If
            Return (0 = Me.CompareTo(eventQueueName))
        End Function

        Private Function FindCorrelationValue(ByVal name As String, ByVal correlationValues As CorrelationProperty()) As Object
            Dim property As CorrelationProperty
            For Each property In correlationValues
                If ((Not [property] Is Nothing) AndAlso ([property].Name = name)) Then
                    Return [property].Value
                End If
            Next
            Return Nothing
        End Function

        Public Function GetCorrelationValues() As CorrelationProperty()
            Return Me.correlationValues
        End Function

        Public Overrides Function GetHashCode() As Integer
            If String.IsNullOrEmpty(Me.activityId) Then
                Return (Me.AssemblyQualifiedName.GetHashCode Xor Me.operation.GetHashCode)
            End If
            Return ((Me.AssemblyQualifiedName.GetHashCode Xor Me.operation.GetHashCode) Xor Me.activityId.GetHashCode)
        End Function

        Public Shared [Operator] =(ByVal queueKey1 As EventQueueName, ByVal queueKey2 As EventQueueName) As Boolean
            Dim flag As Boolean = False
            If (Not queueKey1 Is Nothing) Then
                If (Not queueKey2 Is Nothing) Then
                    flag = (0 = queueKey1.CompareTo(queueKey2))
                End If
                Return flag
            End If
            If (queueKey2 Is Nothing) Then
                flag = True
            End If
            Return flag
        End Function

        Public Shared [Operator] >(ByVal queueKey1 As EventQueueName, ByVal queueKey2 As EventQueueName) As Boolean
            If (queueKey1 Is Nothing) Then
                Throw New ArgumentNullException("queueKey1")
            End If
            If (queueKey2 Is Nothing) Then
                Throw New ArgumentNullException("queueKey2")
            End If
            Return (queueKey1.CompareTo(queueKey2) > 0)
        End Function

        Public Shared [Operator] <>(ByVal queueKey1 As EventQueueName, ByVal queueKey2 As EventQueueName) As Boolean
            Return Not (queueKey1 Is queueKey2)
        End Function

        Public Shared [Operator] <(ByVal queueKey1 As EventQueueName, ByVal queueKey2 As EventQueueName) As Boolean
            If (queueKey1 Is Nothing) Then
                Throw New ArgumentNullException("queueKey1")
            End If
            If (queueKey2 Is Nothing) Then
                Throw New ArgumentNullException("queueKey2")
            End If
            Return (queueKey1.CompareTo(queueKey2) < 0)
        End Function

        Public Overrides Function ToString() As String
            If (Me.stringifiedKey Is Nothing) Then
                Dim builder As New StringBuilder
                builder.AppendLine("Message Properties")
                builder.AppendLine(("Interface Type:" & Me.interfaceType.ToString))
                builder.AppendLine(("Method Name:" & Me.operation.ToString))
                builder.AppendLine("CorrelationValues:")
                If (Not Me.correlationValues Is Nothing) Then
                    Dim property As CorrelationProperty
                    For Each property In Me.correlationValues
                        If ((Not [property] Is Nothing) AndAlso (Not [property].Value Is Nothing)) Then
                            builder.AppendLine([property].Value.ToString)
                        End If
                    Next
                End If
                Me.stringifiedKey = builder.ToString
            End If
            Return Me.stringifiedKey
        End Function


        ' Properties
        Private ReadOnly Property AssemblyQualifiedName As String
            Get
                If (Me.assemblyQualifiedName Is Nothing) Then
                    Me.assemblyQualifiedName = Me.interfaceType.AssemblyQualifiedName
                End If
                Return Me.assemblyQualifiedName
            End Get
        End Property

        Public ReadOnly Property InterfaceType As Type
            Get
                Return Me.interfaceType
            End Get
        End Property

        Public ReadOnly Property MethodName As String
            Get
                Return Me.operation
            End Get
        End Property


        ' Fields
        Private activityId As String
        <NonSerialized> _
        Private assemblyQualifiedName As String
        Private correlationValues As CorrelationProperty()
        Private interfaceType As Type
        Private operation As String
        <NonSerialized> _
        Private stringifiedKey As String
    End Class
End Namespace

